package com.example.service;

import com.example.entity.Logistics;
import com.example.mapper.LogisticsMapper;
import com.example.result.Result;
import com.example.vo.LogisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogisticsService {
    @Autowired
    private LogisticsMapper logisticsMapper;
    //为订单新增物流信息
    public void addLogistics(Logistics logistics) {
        //根据订单ID查询商品对应的商铺
        Integer sId=logisticsMapper.selectSidByOid(logistics.getOrderItemsId());
        //在售后表里面插入售后店铺的ID
        logistics.setStoreId(sId);

        logisticsMapper.addLogistics(logistics);
    }

    //买家查询订单物流
    public Logistics getLogistics(Integer orderId) {
       return logisticsMapper.getLogistics(orderId);
    }


    public Result updateLogistics(List<Logistics> logisticsList, int logisticsStatus,int sId) {
        for (Logistics logistics : logisticsList) {
            if (logistics.getLogisticsStatus()>logisticsStatus) {
                return Result.error("406","所选订单物流状态错误，批量修改失败");
            }
        }
        logisticsMapper.updateLogistics(logisticsList,logisticsStatus,sId);
        return Result.success();
    }


    public Result<List<LogisticsVO>> getLogisticsBySid(Integer sId) {
        List<LogisticsVO> logisticsBySid = logisticsMapper.getLogisticsBySid(sId);
        //根据商铺ID查询物流状态
        return  Result.success(logisticsBySid);
    }
}
